﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;
using System.Data;

namespace WebSite.admin
{
    public partial class index_main : BaseAdmin
    {

        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();



        StringBuilder sbInMoneyList = new StringBuilder();
        StringBuilder sbOrderStatic = new StringBuilder();
        StringBuilder sbOpenCardStatic = new StringBuilder();

        public string InMoneyList
        {
            get
            {
                return sbInMoneyList.ToString();
            }
        }
        public string OrderStatic
        {
            get
            {
                return sbOrderStatic.ToString();
            }
        }
        public string OpenCardStatic
        {
            get
            {
                return sbOpenCardStatic.ToString();
            }
        }

        string sInMoneyFormat = " {1}：<a  onclick=\"openNewTab('outIndDetail','company/OutInDetail.aspx?id={0}','出入账详细-{1}');\" href=\"#\">{2}笔预存款</a></br> ";

        // Methods
        private void Bind()
        {
            string sWhere = string.Empty;
            if (base.CompanyLevelId == "1")
            {
                sWhere = " and OrderCode in (select oa.orderCode from tbOrderAccount oa join DB_Card c on oa.cardId = c.CardId and c.CompanyId = " + base.CompanyId + ")";

            }
            string sNewOrder =  count.counts("", "DB_order", "states", "0" + sWhere);
           string sProcess = count.counts("", "DB_order", "states", "1" + sWhere);
            string  sOut = count.counts("", "DB_order", "states", "2" + sWhere);
             string sComplete = count.counts("", "DB_order", "states", "3" + sWhere);
            sbOrderStatic.AppendFormat("新订单：<a href='#' onclick=\"openNewTab('orderNew','order/orderprocess.aspx?states=0','订单管理');\" >{0}</a></br>", sNewOrder);
            sbOrderStatic.AppendFormat("备货中：<a href='#' onclick=\"openNewTab('orderProcess','order/orderprocess.aspx?states=1','订单管理');\" >{0}</a></br>", sProcess);
            sbOrderStatic.AppendFormat("已发货：<a href='#' onclick=\"openNewTab('orderProcess','order/orderprocess.aspx?states=2','订单管理');\" >{0}</a></br>", sOut);
            sbOrderStatic.AppendFormat("已完成：<a href='#' onclick=\"openNewTab('orderProcess','order/orderprocess.aspx?states=3','订单管理');\" >{0}</a></br>", sComplete);

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["calluser"] != null)
            {
                Response.Redirect("order/OrderAdd.aspx");
            }
            if (!base.IsPostBack)
            {
                this.cPanelInMoney.Hidden = true;
                this.ValidateLoginRule();
                if (Request.QueryString["uid"] == AdminUserId)
                {
                    Response.Write("<script t language=javascript>window.location.href='user/adduser.aspx?uid=" + AdminUserId + "';</script>");
                }
                if (base.GetValidateLoginRule(ERule.CompanyMoneyList))
                {
                    this.cPanelInMoney.Hidden = false;
                    this.BindInMoney();
                }

                if (base.GetValidateLoginRule(ERule.OpenCardManager))
                {

                    this.panelOpenCard.Hidden = false;

                    string sApplicationCard = this.BindApplicationCard("6");

                    string sPass = this.BindApplicationCard("1");
                    sbOpenCardStatic .AppendFormat("申请激活中：<a href='#' onclick=\"openNewTab('applicationCard','product/OpenCard.aspx?states=6','开卡管理');\" >{0}</a></br>", sApplicationCard);
                    sbOpenCardStatic.AppendFormat("审核通过已激活：<a href='#' onclick=\"cardPass('applicationCard','product/OpenCard.aspx?states=1','开卡管理');\" >{0}</a></br>", sPass);

                  

                }
            }
        }


        void ValidateLoginRule()
        {
            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);

            DataSet ds;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                    if (oERule == ERule.OrderProcess)
                    {

                        this.formOrder.Hidden = false;
                        this.Bind();
                        return;
                    }
                }
            }
        }

        string BindApplicationCard(string sStates)
        {


            string sqlCount = " select count(1)  ";
            string sqlBefor = "select ROW_NUMBER() OVER (ORDER BY cardcode asc, starttime desc, cardid asc) AS RowNumber,*,(select [name] from tbCardState where Id = db_card.states) as statesName,convert(varchar(10),starttime,121) + ' 至 ' + convert(varchar(10),endtime,121) as exptime, (select Name from tbCardType where Id = DB_Card.CardTypeId) as CardTypeName"
                          + " ,(select CompanyShortName from tbCompany where Id=DB_Card.companyId  ) + '->' + isnull((select CompanyShortName from tbCompany where Id=DB_Card.IICompanyId  ),' ') + '->' + isnull((select CompanyShortName from tbCompany where Id=DB_Card.IIICompanyId  ),' ') + '->' + isnull((select CompanyShortName from tbCompany where Id=DB_Card.IVCompanyId  ),' ') as companyName"
                           + ", case (select top 1 1 from tbGMS where cardid=DB_Card.cardid) when 1 then '已发' else '未发' end as isSend ";
            string sql = " from DB_Card where 1=1  ";

            if (sStates == "1")
            {
                sql = sql + " and states in (1,5)";
            }
            else if (sStates == "6")
            {
                sql = sql + " and states = 6 ";
            }


            if (!string.IsNullOrEmpty(base.CompanyId))
            {
                sql = sql + string.Format(" and (companyId={0} or IICompanyId={1} or IIICompanyId={2} or IVCompanyId={3})"
                                           , base.CompanyId
                                           , base.CompanyId
                                           , base.CompanyId
                                           , base.CompanyId);
            }

            sqlCount = sqlCount + sql;
            sql = sqlBefor + sql;

            DataTable dtRowCount = SQLAccess.GreatDs(sqlCount).Tables[0];
            return dtRowCount.Rows[0][0].ToString();
        }
        void BindInMoney()
        {
            DataSet ds = theCompany.GetNewInMoneyList(DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));
            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
              
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (base.CompanyId == null || base.CompanyId == dr["ParentId"].ToString())
                    {

                      
                        sbInMoneyList.AppendFormat(sInMoneyFormat
                                                   , dr["CompanyId"].ToString()
                                                   , dr["CompanyName"].ToString()
                                                   , dr["InMoneyCount"].ToString()
                                                   );
                     
                    }
                }

            }
        }


    }
}